/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web.front;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.servlet.ValidateCodeServlet;
import com.thinkgem.jeesite.common.utils.CommonParameter;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.advertising.service.AdvertisingPohotoService;
import com.thinkgem.jeesite.modules.cms.entity.Article;
import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.cms.entity.Comment;
import com.thinkgem.jeesite.modules.cms.entity.Link;
import com.thinkgem.jeesite.modules.cms.entity.Site;
import com.thinkgem.jeesite.modules.cms.service.ArticleDataService;
import com.thinkgem.jeesite.modules.cms.service.ArticleService;
import com.thinkgem.jeesite.modules.cms.service.CategoryService;
import com.thinkgem.jeesite.modules.cms.service.CommentService;
import com.thinkgem.jeesite.modules.cms.service.LinkService;
import com.thinkgem.jeesite.modules.cms.service.SiteService;
import com.thinkgem.jeesite.modules.cms.utils.CmsUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 网站Controller
 * 
 * @author ThinkGem
 * @version 2013-5-29
 */
@Controller
@RequestMapping(value = "${frontPath}")
public class FrontController extends BaseController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleDataService articleDataService;
    @Autowired
    private LinkService linkService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SiteService siteService;
    @Autowired
    private AdvertisingPohotoService advertisingPohotoService;

    /**
     * 网站首页
     */
    @RequestMapping
    public String index(Model model) {
        Site site = CmsUtils.getSite(Site.defaultSiteId());
        model.addAttribute("site", site);
        model.addAttribute("isIndex", true);
        model.addAttribute("uploadPath", CommonParameter.uploadPath);
        model.addAttribute("advertisingPohoto",
                advertisingPohotoService.getBottom());
        //首页获取公司介绍信息
        Article article=new Article();
        String name="公司介绍";
        Category categoryOne=categoryService.queryByName(name);
        if(categoryOne!=null){
        	article.setCategory(categoryOne);
        	 List<Article> list=articleService.findList(article);
             if(list.size()>0){
            	article.setArticleData(articleDataService.get(list.get(0).getId()));
                 model.addAttribute("article",article);
             }
        }
      //首页获取合作伙伴信息
        Link link=new Link();
        String categoryName="合作伙伴";
        Category categoryTwo=categoryService.queryByName(categoryName);
        if(categoryTwo!=null){
        	link.setCategory(categoryTwo);
        	 List<Link> list=linkService.findIndexList(link);
             if(list.size()>0){
                 model.addAttribute("linkList",list);
             }
        }       
        
        //首页获取联系我们信息
        Article articleContact=new Article();
        String nameContact="联系我们";
        Category categoryContact=categoryService.queryByName(nameContact);
        if(categoryOne!=null){
        	articleContact.setCategory(categoryContact);
        	 List<Article> list=articleService.findList(articleContact);
             if(list.size()>0){
            	 articleContact.setArticleData(articleDataService.get(list.get(0).getId()));
                 model.addAttribute("articleContact",articleContact);
             }
        }
        return "modules/cms/front/themes/" + site.getTheme() + "/frontIndex";
    }
    /**
           * 管理登录
           */
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/account/login";
	}     
	
	 /**
     * 管理登录
     */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request, HttpServletResponse response, Model model) {
	    request.getSession().invalidate();
	    Cookie[] cookies = request.getCookies();
	    if(cookies!=null&&cookies.length>0){
	        cookies[0].setMaxAge(0);
	    }
		UserUtils.getSubject().logout();
		
		return "redirect:" + frontPath + "/account/login.html";
	}     

    @RequestMapping("index")
    public String index1(Model model) {
        Site site = CmsUtils.getSite(Site.defaultSiteId());
        model.addAttribute("site", site);
        model.addAttribute("isIndex", true);
        model.addAttribute("uploadPath", CommonParameter.uploadPath);
        model.addAttribute("advertisingPohoto",
                advertisingPohotoService.getBottom());
        return "modules/cms/front/themes/" + site.getTheme() + "/frontIndex";
    }

    /**
     * 网站首页
     */
    @RequestMapping(value = "index-{siteId}${urlSuffix}")
    public String index(@PathVariable String siteId, Model model) {
        if (siteId.equals("1")) {
            return "redirect:" + Global.getFrontPath();
        }
        Site site = CmsUtils.getSite(siteId);
        // 子站有独立页面，则显示独立页面
        if (StringUtils.isNotBlank(site.getCustomIndexView())) {
            model.addAttribute("site", site);
            model.addAttribute("isIndex", true);
            return "modules/cms/front/themes/" + site.getTheme() + "/frontIndex"
                    + site.getCustomIndexView();
        }
        // 否则显示子站第一个栏目
        List<Category> mainNavList = CmsUtils.getMainNavList(siteId);
        if (mainNavList.size() > 0) {
            String firstCategoryId = CmsUtils.getMainNavList(siteId).get(0)
                    .getId();
            return "redirect:" + Global.getFrontPath() + "/list-"
                    + firstCategoryId + Global.getUrlSuffix();
        } else {
            model.addAttribute("site", site);
            return "modules/cms/front/themes/" + site.getTheme()
                    + "/frontListCategory";
        }
    }

    /**
     * 内容列表
     */
    @RequestMapping(value = "list-{categoryId}${urlSuffix}")
    public String list(@PathVariable String categoryId,Date updateDate,
            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(required = false, defaultValue = "15") Integer pageSize,
            Model model) {
        Category category = categoryService.get(categoryId);
        if (category == null) {
            Site site = CmsUtils.getSite(Site.defaultSiteId());
            model.addAttribute("site", site);
            return "error/404";
        }
        Site site = siteService.get(category.getSite().getId());
        model.addAttribute("site", site);
        // 2：简介类栏目，栏目第一条内容
        if ("2".equals(category.getShowModes())
                && "article".equals(category.getModule())) {
            // 如果没有子栏目，并父节点为跟节点的，栏目列表为当前栏目。
            List<Category> categoryList = Lists.newArrayList();
            if (category.getParent().getId().equals("1")) {
                categoryList.add(category);
            } else {
                categoryList = categoryService.findByParentId(
                        category.getParent().getId(),
                        category.getSite().getId());
            }
            model.addAttribute("category", category);
            model.addAttribute("categoryList", categoryList);
            // 获取文章内容
            Page<Article> page = new Page<Article>(1, 1, -1);
            Article article = new Article(category);
            page = articleService.findPage(page, article, false);
            if (page.getList().size() > 0) {
                article = page.getList().get(0);
                article.setArticleData(articleDataService.get(article.getId()));
                articleService.updateHitsAddOne(article.getId());
            }
            model.addAttribute("article", article);
            CmsUtils.addViewConfigAttribute(model, category);
            CmsUtils.addViewConfigAttribute(model, article.getViewConfig());
            return "modules/cms/front/themes/" + site.getTheme() + "/"
                    + getTpl(article);
        } else {
            List<Category> categoryList = categoryService.findByParentId(
                    category.getId(), category.getSite().getId());
            // 展现方式为1 、无子栏目或公共模型，显示栏目内容列表
            if ("1".equals(category.getShowModes())
                    || categoryList.size() == 0) {
                // 有子栏目并展现方式为1，则获取第一个子栏目；无子栏目，则获取同级分类列表。
                if (categoryList.size() > 0) {
                    category = categoryList.get(0);
                } else {
                    // 如果没有子栏目，并父节点为跟节点的，栏目列表为当前栏目。
                    if (category.getParent().getId().equals("1")) {
                        categoryList.add(category);
                    } else {
                        categoryList = categoryService.findByParentId(
                                category.getParent().getId(),
                                category.getSite().getId());
                    }
                }
                model.addAttribute("category", category);
                model.addAttribute("categoryList", categoryList);
                // 获取内容列表
                if ("article".equals(category.getModule())) {//
                    Page<Article> page = new Page<Article>(pageNo, pageSize);
                    Article art= new Article(category);
                   if(updateDate!=null){
                       art.setUpdateDate(updateDate); 
                       model.addAttribute("updateDateHidden",updateDate);
                   }
                    
                    page = articleService.findPage(page, art,false);
                    List<Article> list = page.getList();
                    /*   List<Article> list1 = new ArrayList<Article>();
                    if(updateDate!=null){
                        if(!list.isEmpty()){
                            for(Article arts:list){
                                if(dateTransitionString(arts.getUpdateDate()).equals(updateDate)){
                                    list1.add(arts);
                                }
                            } 
                        }
                    }*/
                    
                    
                    model.addAttribute("dateList", getDateList(list));
                    model.addAttribute("page", page);
                    // 如果第一个子栏目为简介类栏目，则获取该栏目第一篇文章
                    if ("2".equals(category.getShowModes())) {
                        Article article = new Article(category);
                        if (page.getList().size() > 0) {
                            article = page.getList().get(0);
                            article.setArticleData(
                                    articleDataService.get(article.getId()));
                            articleService.updateHitsAddOne(article.getId());
                        }
                        model.addAttribute("article", article);
                        CmsUtils.addViewConfigAttribute(model, category);
                        CmsUtils.addViewConfigAttribute(model,
                                article.getViewConfig());
                        return "modules/cms/front/themes/" + site.getTheme()
                                + "/" + getTpl(article);
                    }
                } else if ("link".equals(category.getModule())) {
                    Page<Link> page = new Page<Link>(1, -1);
                    page = linkService.findPage(page, new Link(category),
                            false);
                    model.addAttribute("page", page);
                }
                String view = "/frontList";
                if (StringUtils.isNotBlank(category.getCustomListView())) {
                    view = "/" + category.getCustomListView();
                }
                CmsUtils.addViewConfigAttribute(model, category);
                site = siteService.get(category.getSite().getId());
                // System.out.println("else 栏目第一条内容 _2
                // :"+category.getSite().getTheme()+","+site.getTheme());
                return "modules/cms/front/themes/"
                        + siteService.get(category.getSite().getId()).getTheme()
                        + view;
                // return
                // "modules/cms/front/themes/"+category.getSite().getTheme()+view;
            }
            // 有子栏目：显示子栏目列表
            else {
                model.addAttribute("category", category);
                model.addAttribute("categoryList", categoryList);
                String view = "/frontListCategory";
                if (StringUtils.isNotBlank(category.getCustomListView())) {
                    view = "/" + category.getCustomListView();
                }
                CmsUtils.addViewConfigAttribute(model, category);
                return "modules/cms/front/themes/" + site.getTheme() + view;
            }
        }
    }

    public Set<String> getDateList(List<Article> list){
        Set<String> dateList = new HashSet<String>();
        if(!list.isEmpty()){
            for(Article art:list){
                if(art.getUpdateDate()!=null){
                    dateList.add(dateTransitionString(art.getUpdateDate())); 
                }
            }  
        }
        return dateList;
    }
    
    
    /**日期转型*/
    public String dateTransitionString(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM");
        String ss =formatter.format(date);
        return formatter.format(date);
    }
    
    
    /**
     * 内容列表（通过url自定义视图）
     */
    @RequestMapping(value = "listc-{categoryId}-{customView}${urlSuffix}")
    public String listCustom(@PathVariable String categoryId,
            @PathVariable String customView,
            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(required = false, defaultValue = "15") Integer pageSize,
            Model model) {
        Category category = categoryService.get(categoryId);
        if (category == null) {
            Site site = CmsUtils.getSite(Site.defaultSiteId());
            model.addAttribute("site", site);
            return "error/404";
        }
        Site site = siteService.get(category.getSite().getId());
        model.addAttribute("site", site);
        List<Category> categoryList = categoryService
                .findByParentId(category.getId(), category.getSite().getId());
        model.addAttribute("category", category);
        model.addAttribute("categoryList", categoryList);
        CmsUtils.addViewConfigAttribute(model, category);
        return "modules/cms/front/themes/" + site.getTheme()
                + "/frontListCategory" + customView;
    }

    /**
     * 显示内容
     */
    @RequestMapping(value = "view-{categoryId}-{contentId}${urlSuffix}")
    public String view(@PathVariable String categoryId,
            @PathVariable String contentId, Model model) {
        Category category = categoryService.get(categoryId);
        if (category == null) {
            Site site = CmsUtils.getSite(Site.defaultSiteId());
            model.addAttribute("site", site);
            return "error/404";
        }
        model.addAttribute("site", category.getSite());
        if ("article".equals(category.getModule())) {
            // 如果没有子栏目，并父节点为跟节点的，栏目列表为当前栏目。
            List<Category> categoryList = Lists.newArrayList();
            if (category.getParent().getId().equals("1")) {
                categoryList.add(category);
            } else {
                categoryList = categoryService.findByParentId(
                        category.getParent().getId(),
                        category.getSite().getId());
            }
            // 获取文章内容
            Article article = articleService.get(contentId);
            if (article == null
                    || !Article.DEL_FLAG_NORMAL.equals(article.getDelFlag())) {
                return "error/404";
            }
            // 文章阅读次数+1
            articleService.updateHitsAddOne(contentId);
            // 获取推荐文章列表
            List<Object[]> relationList = articleService.findByIds(
                    articleDataService.get(article.getId()).getRelation());
            // 将数据传递到视图
            model.addAttribute("category",
                    categoryService.get(article.getCategory().getId()));
            model.addAttribute("categoryList", categoryList);
            article.setArticleData(articleDataService.get(article.getId()));
            model.addAttribute("article", article);
            model.addAttribute("relationList", relationList);
            CmsUtils.addViewConfigAttribute(model, article.getCategory());
            CmsUtils.addViewConfigAttribute(model, article.getViewConfig());
            Site site = siteService.get(category.getSite().getId());
            model.addAttribute("site", site);
            // return
            // "modules/cms/front/themes/"+category.getSite().getTheme()+"/"+getTpl(article);
            return "modules/cms/front/themes/" + site.getTheme() + "/"
                    + getTpl(article);
        }
        return "error/404";
    }

    /**
     * 内容评论
     */
    @RequestMapping(value = "comment", method = RequestMethod.GET)
    public String comment(String theme, Comment comment,
            HttpServletRequest request, HttpServletResponse response,
            Model model) {
        Page<Comment> page = new Page<Comment>(request, response);
        Comment c = new Comment();
        c.setCategory(comment.getCategory());
        c.setContentId(comment.getContentId());
        c.setDelFlag(Comment.DEL_FLAG_NORMAL);
        page = commentService.findPage(page, c);
        model.addAttribute("page", page);
        model.addAttribute("comment", comment);
        return "modules/cms/front/themes/" + theme + "/frontComment";
    }

    /**
     * 内容评论保存
     */
    @ResponseBody
    @RequestMapping(value = "comment", method = RequestMethod.POST)
    public String commentSave(Comment comment, String validateCode,
            @RequestParam(required = false) String replyId,
            HttpServletRequest request) {
        if (StringUtils.isNotBlank(validateCode)) {
            if (ValidateCodeServlet.validate(request, validateCode)) {
                if (StringUtils.isNotBlank(replyId)) {
                    Comment replyComment = commentService.get(replyId);
                    if (replyComment != null) {
                        comment.setContent("<div class=\"reply\">"
                                + replyComment.getName() + ":<br/>"
                                + replyComment.getContent() + "</div>"
                                + comment.getContent());
                    }
                }
                comment.setIp(request.getRemoteAddr());
                comment.setCreateDate(new Date());
                comment.setDelFlag(Comment.DEL_FLAG_AUDIT);
                commentService.save(comment);
                return "{result:1, message:'提交成功。'}";
            } else {
                return "{result:2, message:'验证码不正确。'}";
            }
        } else {
            return "{result:2, message:'验证码不能为空。'}";
        }
    }

    /**
     * 站点地图
     */
    @RequestMapping(value = "map-{siteId}${urlSuffix}")
    public String map(@PathVariable String siteId, Model model) {
        Site site = CmsUtils
                .getSite(siteId != null ? siteId : Site.defaultSiteId());
        model.addAttribute("site", site);
        return "modules/cms/front/themes/" + site.getTheme() + "/frontMap";
    }

    private String getTpl(Article article) {
        if (StringUtils.isBlank(article.getCustomContentView())) {
            String view = null;
            Category c = article.getCategory();
            boolean goon = true;
            do {
                if (StringUtils.isNotBlank(c.getCustomContentView())) {
                    view = c.getCustomContentView();
                    goon = false;
                } else if (c.getParent() == null || c.getParent().isRoot()) {
                    goon = false;
                } else {
                    c = c.getParent();
                }
            } while (goon);
            return StringUtils.isBlank(view) ? Article.DEFAULT_TEMPLATE : view;
        } else {
            return article.getCustomContentView();
        }
    }

}
